package org.dong.control;

import org.dong.pojo.Dto;
import org.dong.pojo.User;
import org.dong.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserControl extends HttpServlet {

    @Resource
    UserService userService;
    @RequestMapping("/User/login")
    public Dto checkLogin(HttpServletRequest req, HttpServletResponse resp){
        Dto dto = new Dto();
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String code = req.getParameter("code");


        if (code.equalsIgnoreCase((String) req.getSession().getAttribute("codeStr"))) {
            User user = userService.checkLogin(account, password);
            if (user != null) {
                req.getSession().setAttribute("user",user);

                Map<String, Object> datas = new HashMap<String, Object>();

                datas.put("user", user);
                dto.setDatas(datas);
                dto.setLocation("main");
                dto.setErrMsg("登录成功");
                dto.setStatusCode(200);

            } else {
                dto.setErrMsg("登录失败");
                dto.setStatusCode(2001);
            }
        } else {
            dto.setErrMsg("验证码错误");
            dto.setStatusCode(2001);
        }


        return dto;
    }
}
